import axios from "axios"
import { showNotification } from "@/router/index.js"
const instance = axios.create({
  //   baseURL: 'http://localhost:8080/api'
  baseURL: "/api",
  withCredentials: true, // 跨域请求时发送Cookie
})
export let loading = false

instance.interceptors.request.use(
  (config) => {
    loading = true
    return config
  },
  (err) => {
    console.log(err)
    showNotification("请求之前的报错", err.message, 10000, "top-left", "error")
    return Promise.reject(err)
  }
)
instance.interceptors.response.use(
  (res) => {
    if (res.status == 200) {
      if (+res.data.code !== 0) {
        showNotification("请求结果中的报错", res.data.message, 10000, "top-left", "error")
      }
      showNotification("请求成功,下面是请求结果", JSON(res.data), 10000, "top-left", "success")
      loading = false
      return res.data
    }
  },
  (err) => {
    // console.log(err)
    loading = false
    showNotification("请求结果中的报错", err.message, 10000, "top-left", "error")
    return Promise.reject(err)
  }
)

export default instance
